package uiLayer;

import java.util.ArrayList;
import java.util.Scanner;

import modelLayer.Customer;
import modelLayer.Employee;
import modelLayer.SaleLinesItem;
import controllerLayer.CustomerCtr;
import controllerLayer.EmployeeCtr;
import controllerLayer.LoanCtr;
import controllerLayer.ProductCtr;
import controllerLayer.SaleCtr;

public class DIYUI {
	private ArrayList<SaleLinesItem> sls;
	private static DIYUI instance = null;
	public LoanCtr lctr;
	public EmployeeCtr ectr;
	public CustomerCtr cctr;
	public SaleCtr sctr;
	public ProductCtr pctr;
	
	public DIYUI() {
		sls = new ArrayList<SaleLinesItem>();
		lctr = new LoanCtr();
		cctr = new CustomerCtr(); 
		ectr = new EmployeeCtr();
		sctr = new SaleCtr();
		pctr = new ProductCtr();
	}
	public static DIYUI getInstance(){
		if(instance==null){
			instance = new DIYUI();
		}
		return instance;
		}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DIYUI instance = new DIYUI();
		instance.run();

	}

	public void run() {
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Sale or Lease?");
			System.out.println("1. Sale");
			System.out.println("2. Lease");
			if(keyboard.nextLine().equals("2"))runLease();
			else runSale();
		}

	public void runSale() {
		int id = 0;
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Employee CPR:");
		String empId = keyboard.nextLine();
		Employee employee = ectr.getEmployee(empId);
		System.out.println("Customer CPR:");
		String custId = keyboard.nextLine();
		Customer customer = cctr.getCustomer(custId);
		sls = typeProducts();
		id = sctr.createOffer(customer,employee,sls);
		System.out.println(id);
		System.out.println(sctr.findSale(id).printOffer(id));
		printOrder(id);
	}
	public void printOrder(int id) {
		System.out.println("Order"+sctr.findSale(id).printOffer(id));
	
	}
		
	public ArrayList<SaleLinesItem> typeProducts() {
		ArrayList<SaleLinesItem> ss = new ArrayList<SaleLinesItem>();
		Scanner keyboard = new Scanner(System.in);
		boolean finish = false;
		while(!finish){
		System.out.println("Sale Lines Item ID: ");
		String ID = keyboard.nextLine();
		System.out.println("Quantity:");
		Double quantity = InputParser.getInstance().parseDouble(keyboard.next());
		try{
			SaleLinesItem s1 = pctr.getSaleLinesItem(ID);
		s1.setQuantity(quantity);
		ss.add(s1);
		}
		catch(NullPointerException npe){
			System.out.println("Item doesn't exist");
		}
		System.out.println("if you want to add new product press 1, if not press 0");
		if(keyboard.next().equals("0"))finish = true;
		}
		try{
		return ss;
		}
		catch(NullPointerException npe){
			return null;
		}
		}
	

	

	public void runLease() {
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Employee CPR:");
		String empId = keyboard.nextLine();
		System.out.println("Customer CPR:");
		String custId = keyboard.nextLine();
		System.out.println("Product ID:");
		int Id = InputParser.getInstance().parseInt(keyboard.nextLine());
		System.out.println("Borrow Date:");
		String borrowDate = keyboard.nextLine();
		System.out.println("Period:");
		int period = InputParser.getInstance().parseInt(keyboard.nextLine());
		try{
			lctr.loanProduct(custId, Id, empId, borrowDate, period);
			DIYUI.getInstance().run();
		}
		catch(NullPointerException npe){
			
				System.out.println("Something went wrong!");
				OfficeUI.getInstance().run();
			}
		}
		
	}
		
	


